package com.um.pasantiaum.empresa.pedidos;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.parse.ParseException;
import com.parse.SaveCallback;
import com.um.pasantiaum.R;
import com.um.pasantiaum.objetos.Pedido;
import com.um.pasantiaum.objetos.SelectedPedido;

public class DetailPedidoNewActivity extends Activity {

	final Context context = this;
	private Pedido selectedOrder;
	private TextView mDate, mUserName, mProducts, mTotal;
	private Button mAceptar, mRechazar, mVerCamion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_detail_pedido);
		selectedOrder = SelectedPedido.getSelectedPedido().getPedido();

		mDate = (TextView) findViewById(R.id.producto_date_detail);
		mDate.setText(selectedOrder.getDateCreated().toString());

		mUserName = (TextView) findViewById(R.id.prodructo_nombre_detail);
		mUserName.setText(selectedOrder.getCliente().getName());

		mProducts = (TextView) findViewById(R.id.producto_descripcion_detail);
		mProducts.setText(selectedOrder.getProductos());

		mTotal = (TextView) findViewById(R.id.producto_precio_detail);
		mTotal.setText("$ " + String.valueOf(selectedOrder.getPrecioTotal()));

		mAceptar = (Button) findViewById(R.id.aceptar_button_detail);
		mAceptar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						context);
				// set title
				alertDialogBuilder.setTitle("Aceptar pedido");

				// set dialog message
				alertDialogBuilder
						.setMessage(
								"Esta seguro que desea aceptar este pedido?")
						.setCancelable(false)
						.setPositiveButton("Acpetar",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										// if this button is clicked, close
										// current activity
										final ProgressDialog dlg = new ProgressDialog(
												DetailPedidoNewActivity.this);
										dlg.setTitle("Por favor espere");
										dlg.setMessage("Actualizando");
										dlg.show();
										selectedOrder.getPedidoEnParse().put(
												"estado", "open");

										selectedOrder.getPedidoEnParse()
												.saveInBackground(
														new SaveCallback() {

															@Override
															public void done(
																	ParseException e) {
																dlg.dismiss();
																DetailPedidoNewActivity.this
																		.finish();

															}
														});

									}
								})
						.setNegativeButton("Cancelar",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										// if this button is clicked, just close
										// the dialog box and do nothing
										dialog.cancel();
									}
								});

				// create alert dialog
				AlertDialog alertDialog = alertDialogBuilder.create();

				// show it
				alertDialog.show();

			}
		});

		mRechazar = (Button) findViewById(R.id.rechazar_button_detail);
		mRechazar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						context);
				// set title
				alertDialogBuilder.setTitle("Rechazar pedido");

				// set dialog message
				alertDialogBuilder
						.setMessage(
								"Esta seguro que desea rechazar este pedido?")
						.setCancelable(false)
						.setPositiveButton("Rechazar",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										// if this button is clicked, close
										// current activity
										final ProgressDialog dlg = new ProgressDialog(
												DetailPedidoNewActivity.this);
										dlg.setTitle("Por favor espere");
										dlg.setMessage("Actualizando");
										dlg.show();
										selectedOrder.getPedidoEnParse().put(
												"estado", "declined");

										selectedOrder.getPedidoEnParse()
												.saveInBackground(
														new SaveCallback() {

															@Override
															public void done(
																	ParseException e) {
																dlg.dismiss();
																DetailPedidoNewActivity.this
																		.finish();

															}
														});

									}
								})
						.setNegativeButton("Cancelar",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										// if this button is clicked, just close
										// the dialog box and do nothing
										dialog.cancel();
									}
								});

				// create alert dialog
				AlertDialog alertDialog = alertDialogBuilder.create();

				// show it
				alertDialog.show();				
			}
		});
		
		mVerCamion = (Button) findViewById(R.id.ver_camion_button_detail);
		mVerCamion.setVisibility(View.INVISIBLE);

	}

}
